﻿<?xml version="1.0" encoding="utf-8"?>
<!-- 
    Spun AJAX Framework
    Copyright (C) 2008 Blake Hegerle

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:html="http://www.w3.org/1999/xhtml"
                xmlns:view="http://spun.googlecode.com/svn/tags/2008-02-01/Views.xsd">

    <xsl:output method="xml"
                indent="yes"
                version="1.0"
                encoding="utf-8"
                omit-xml-declaration="no"
                doctype-public="-//W3C//DTD XHTML 1.1//EN"
                doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>

    <xsl:template match="html:html">
        <html>
            <head>
                <xsl:apply-templates select="html:head" />

                <script type="text/javascript"
                        src="http://www.prototypejs.org/assets/2008/1/25/prototype-1.6.0.2.js" />
                <script type="text/javascript" src="Content/Spun-1.0.0.js" />
            </head>

            <xsl:apply-templates select="html:body" />
        </html>
    </xsl:template>

    <xsl:template match="html:body">
        <xsl:copy>
            <xsl:attribute name="onload">
                <xsl:text>StartSpun();</xsl:text>
            </xsl:attribute>

            <xsl:apply-templates select="*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:for-each select="@*">
                <xsl:copy />
            </xsl:for-each>

            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="view:inline">
        <xsl:variable name="ID" select="generate-id(.)" />

        <xsl:if test="@path">
            <span id="{$ID}">
                <xsl:call-template name="register">
                    <xsl:with-param name="updateMethod" select="'scalarUpdate'" />
                    <xsl:with-param name="path" select="@path" />
                    <xsl:with-param name="ID" select="$ID" />
                </xsl:call-template>
            </span>
        </xsl:if>
    </xsl:template>

    <xsl:template name="register">
        <xsl:param name="ID" />
        <xsl:param name="path" />
        <xsl:param name="updateMethod" />

        <script type="text/javascript">
            <xsl:text>register('</xsl:text>
            <xsl:value-of select="$ID" />
            <xsl:text>', '</xsl:text>
            <xsl:value-of select="$path"/>
            <xsl:text>', </xsl:text>
            <xsl:value-of select="$updateMethod" />
            <xsl:text>);</xsl:text>
        </script>
    </xsl:template>
</xsl:stylesheet>
